<?php

namespace app\index\controller;

use think\Controller;
use think\facade\Request;
use app\index\model\Warehouse as Model;
use app\index\logic\Warehouse as Logic;
use app\index\model\House;
class Warehouse extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        if(Request::isPost()) {
            $where=[];
            $name = Request::post('name');

            if($name)
            {
                $where[] = ['name','like',"%{$name}%"];
            }

            $wareHouses= Model::where($where)->paginate(10);
            $total = Model::where($where)->count();
            returnAjax('200',[
                'data'=>$wareHouses->items(),
                'total'=>$total,
                'page'=>input('get.page',1)
            ]);

        }

        return $this->fetch('',compact('wareHouses'));
    }

    /**
     * 添加仓库.
     *
     * @return \think\Response
     */
    public function create()
    {
        if(Request::isPost()) {
            $Validate = new \app\index\validate\Warehouse();
            $post = input('post.');
            if(!$Validate->check($post)){
                returnAjax('201',$Validate->getError());
            }
            if(Model::isHasRecord()){
                returnAjax('201','代码或者仓库名已存在');
            }
            $post['id'] = $this->guid();
            $latAndLng = $this->getLatlng( $post['address'] );  //获取经纬度
            if( $latAndLng['lat']==0 )
            {
                returnAjax('201','读取经纬度失败');
            }
            $post['lat'] = $latAndLng['lat'];
            $post['lng'] = $latAndLng['lng'];
            Model::create($post);
            returnAjax('200','');
        }
        return $this->fetch();
    }





    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update()
    {
         if(Request::isPost()) {
             $Validate = new \app\index\validate\Warehouse();
             $post = input('post.');
             if(!$Validate->check($post)){
                 returnAjax('201',$Validate->getError());
             }
             if(Model::isHasRecord()){
                 returnAjax('201','代码或者仓库名已存在');
             }
             $latAndLng = $this->getLatlng( $post['address'] );  //获取经纬度
             if( $latAndLng['lat']==0 )
             {
                 returnAjax('201','读取经纬度失败');
             }
             $post['lat'] = $latAndLng['lat'];
             $post['lng'] = $latAndLng['lng'];
             Model::update($post);
             returnAjax('200','');
         }
         $id = Request::get('id');
         $warehouse = Model::find($id);
         return $this->fetch('',compact('warehouse'));
    }


    /**
     * 仓库权限列表页面
     * @return mixed
     */
    public  function manage(){
        $logic = new Logic();
        $data = $logic->userWareHouse();
        return $this->fetch('',compact('data'));
    }

    /**
     * 显示创建仓库权限页面
     * @return mixed
     */
    public function createManage(){
        $wareHouses = Model::where(['Status'=>1])->select();
        return $this->fetch('',compact('wareHouses'));
    }

    /**
     * 保存仓库权限数据
     */
    public function stroeManage(){
        if(Request::isPost()) {
             $post =  Request::post();
             if(!isset($post['UserID'])) {
                 returnAjax('201','用户不能为空');
             }
             $res= House::where(['WareHouse'=>$post['WareHouse']])->find();
             if($res) {  //存在记录更新记录
                 //判断是否已经存在记录避免重复写入

                 if(!in_array($post['UserID'],explode(',',$res['AllowUser']))) {
                     House::where(['WareHouse'=>$post['WareHouse']])->update(['AllowUser'=>$res['AllowUser'].','.$post['UserID']]);
                 }
             }else{  //不存在增加记录
                 House::insert(['WareHouse'=>$post['WareHouse'],'Name'=>$post['Name'],'AllowUser'=>$post['UserID']]);
             }

             returnAjax('200','修改成功');

        }
    }
    /**
     * 剔除仓库权限数据
     */
    public function removeManage(){
           $AllowUser = Request::post('AllowUser');
           $WareHouse= Request::post('WareHouse');
           if(!$AllowUser){
               returnAjax('201','缺少AllowUser参数');
           }
           if(!$WareHouse){
              returnAjax('201','缺少WareHouser参数');
           }
           //分割WareHouser参数，获取仓库编号
           $WareHouses = explode(' ',$WareHouse);
           $FindAllowUser= House::where(['WareHouse'=>$WareHouses[0]])->value('AllowUser');
           $AllowUsers = explode(',' ,$FindAllowUser);
           if(!in_array($AllowUser,$AllowUsers)) {
               returnAjax('201','删除失败');
           }
           foreach ($AllowUsers as $key=>$val ) {
               if($val==$AllowUser){
                   unset($AllowUsers[$key]);
               }
           }
           $updateAllowUser = implode(',',$AllowUsers);
           $res =  House::where(['WareHouse'=>$WareHouses[0]])->update(['AllowUser'=>$updateAllowUser]);
           if($res){
               returnAjax('200','删除成功');
           }else{
               returnAjax('201','删除失败');
           }
    }

    /**
     * 获取仓库信息
     */
   public function getwareHouse()
   {
       $wareHouse = Request::post('warehouse');
       $wareHouseCenter = \app\index\model\Warehouse::where(['location_code'=>$wareHouse])->find();
       returnAjax('200',$wareHouseCenter->toArray());
   }
}
